package com.memo.activity;

import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;
import androidx.loader.content.CursorLoader;

import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.provider.MediaStore;
import android.text.Editable;
import android.text.Html;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.ImageSpan;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.MimeTypeMap;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.memo.Eventbean.homeone_evetbean;
import com.memo.R;
import com.memo.SQL.memosql;

import com.memo.Utils.FileUtils;
import com.memo.Utils.ImageUtils;
import com.memo.Utils.ScreenUtils;
import com.memo.model.newedit_model;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;


public class EditActivity extends AppCompatActivity implements View.OnClickListener {
    ImageView edit_activity_back;//返回
    ImageView edit_activity_share;//分享
    ImageView edit_activity_menu;//菜单
    ImageView edit_photo;
    TextView edit_activity_datatime;//详细时间
    EditText edit_activity_title;//标题
    EditText edit_activity_content;//内容
    memosql memosql;
    newedit_model newedit_model;
    int id;

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(homeone_evetbean event) {
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_edit);
        getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);//设置状态栏字体颜色
        id=getIntent().getIntExtra("id",0);
        EventBus.getDefault().register(EditActivity.this);
        edit_activity_back = findViewById(R.id.edit_activity_back);
        edit_activity_share = findViewById(R.id.edit_activity_share);
        edit_activity_menu = findViewById(R.id.edit_activity_menu);
        edit_photo=findViewById(R.id.edit_activity_bottom_menu).findViewById(R.id.edit_photo);
        edit_activity_back.setOnClickListener(this);
        edit_activity_share.setOnClickListener(this);
        edit_activity_menu.setOnClickListener(this);
        edit_photo.setOnClickListener(this);

        edit_activity_title = findViewById(R.id.edit_activity_title);
        edit_activity_datatime = findViewById(R.id.edit_activity_datatime);
        edit_activity_content = findViewById(R.id.edit_activity_content);

        memosql=new memosql(this);
        newedit_model=new newedit_model(this);


        Log.i("onCreate: ",""+id);
        if (id==0){
            newedit_model.Start(edit_activity_datatime,edit_activity_content);
        }else {
            try {
                newedit_model.loadStart(id,memosql,edit_activity_datatime,edit_activity_title,edit_activity_content);
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
    }

    Html.ImageGetter imgGetter = new Html.ImageGetter() {
        public Drawable getDrawable(String source) {
            Drawable drawable = null;
            drawable = Drawable.createFromPath(source); //显示本地图片
            drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable
                    .getIntrinsicHeight());
            return drawable;
        }
    };

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.edit_activity_back:
                newedit_model.activityback(id,memosql,edit_activity_title,edit_activity_content);
                break;
            case R.id.edit_activity_share:
                break;
            case R.id.edit_activity_menu:
                break;
            case R.id.edit_photo:
                newedit_model.callGallery();
                break;
        }
    }

    @RequiresApi(api = Build.VERSION_CODES.Q)
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 123) {
            try {
                // 获得图片的uri
                Uri originalUri = data.getData();
                Editable editable=edit_activity_content.getText();
                editable.insert(edit_activity_content.getSelectionStart(),Html.fromHtml("<img src=\""+FileUtils.uriToFileApiQ(originalUri,this).getPath()+"\"/>",imgGetter,null));
            } catch (Exception e) {
            }
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode==KeyEvent.KEYCODE_BACK){
            newedit_model.activityback(id,memosql,edit_activity_title,edit_activity_content);
        }
        return super.onKeyDown(keyCode, event);
    }
}